import service from '@/utils/request'
import { set } from 'lodash'

export const fetchData = (url, method = 'post') => async data => {
  let requestOption = {
    url: url,
    headers: { Authorization: localStorage.getItem('token') }
  }
  set(requestOption, 'method', method)
  set(requestOption, method === 'get' || method === 'GET' ? 'params' : 'data', data)

  try {
    const res = await service
      .request(requestOption)
    return await Promise.resolve(res)
  } catch (error) {
    throw error
  }
}
